<?php
namespace app\youzhi\controller;



use app\admin\model\Inventoryentry;
use app\admin\model\Material;
use app\admin\model\Surface;
use app\admin\model\Texture;
use think\Cache;

class Crontab
{

    public function run()
    {

        $datacenter = input('datacenter','');

        error_reporting(0);
        ini_set('display_errors', '0');

        $action1= [
            'bizpersoncate'                         => '业务分组',
            'bizperson'                             => '业务员',
            'texturecate'                           => '材质组',
            'texture'                               => '材质',
            'surfacecate'                           => '表面组',
            'surface'                               => '表面',
            'carinfocate'                           => '配送组',
            'carinfo'                               => '配送车',
            'curstomercate'                         => '客户组',
            'curstomer'                             => '客户',
            'ivnentory'                             => '库存',
            'purorder'                              => 'ERP订单',
            'payrecord'                             => '付款记录（已确认）',
            'payrecordno'                           => '付款记录（未通过）',
        ];
        $action = [

            'ivnentory'                             => '库存',
        ];

            $erp           = new Erp();

            foreach ($action1 as $key => $value)
            {
                $erp->run($key,$datacenter);
            }

            $this->updateinventoryentryCategory();
            $this->inventoryIdforMaterTextSurface();

            if(!empty($datacenter))
            {
                return ['code' => 1, 'msg' => '增量同步完成'];
            }
            return "同步完成";


    }

    //3分钟执行
    public function updateinventoryentryCategory()
    {
        //获取已经匹配好的库存id
        $data = Inventoryentry::column('id');
        $data = array_unique($data);

        $inventroy = \app\admin\model\Inventory::limit(10000);

        if(!empty($data))
        {
            $inventroy = $inventroy->where('id','not in',$data);
        }
        //拿到未匹配的库存id
        $inventroy = $inventroy
            ->whereNotNull('fk_bdl_inventoryjsondata')
            ->where('fk_bdl_inventoryjsondata','neq','[]')
            ->select();
        foreach ($inventroy as $k => $v)
        {
            $tk_bdl_bal_inventoryentry = json_decode($v['fk_bdl_inventoryjsondata'],true);

            if(!empty($tk_bdl_bal_inventoryentry))
            {
                foreach ($tk_bdl_bal_inventoryentry as $key => $value)
                {
                    $isExist = \app\admin\model\Inventoryentry::where('id', $v['id'])
                        ->where('forgid', $v['forgid'])
                        ->where('fk_bdl_bd_datacenter', $v['fk_bdl_bd_datacenter'])
                        ->where('fk_bdl_matpropid', $value['fk_bdl_matpropid'])
                        ->find();
                    if(!$this->endsWithZero($value['fk_bdl_matpropvalue']))
                    {

                        $insertData = [
                            'fk_bdl_bd_datacenter'      => $value['fk_bdl_bd_datacenter'],
                            'forgid'                    => $value['forgid'],
                            'fk_bdl_matpropid'          => $value['fk_bdl_matpropid'],
                            'fk_bdl_matpropvalue'       => $value['fk_bdl_matpropvalue'],
                            'id'                        => $v['id']
                        ];
                    }else{
                        $insertData = [
                            'fk_bdl_bd_datacenter'      => $value['fk_bdl_bd_datacenter'],
                            'forgid'                    => $value['forgid'],
                            'fk_bdl_matpropid'          => $value['fk_bdl_matpropid'],
                            'fk_bdl_matpropvalue'       =>  floatval($value['fk_bdl_matpropvalue']),
                            'id'                        => $v['id']
                        ];
                    }



                    if(!empty($isExist))
                    {
                        unset($insertData['id']);
                        Inventoryentry::where('fentryid', $isExist['fentryid'])->update($insertData);
                    }else{

                        Inventoryentry::create($insertData);
                    }

                }


            }

        }


    }

    public function inventoryIdforMaterTextSurface()
    {
        $data = \app\admin\model\Inventory::where('fk_bdl_bd_materialnum','neq',' ')->where('fk_bdl_bd_texturenum','neq',' ')->where('fk_bdl_bd_surfacenum','neq',' ')
            ->whereNull('fk_bdl_bd_material')->select();

        if(empty($data)){$data = \app\admin\model\Inventory::where('fk_bdl_bd_materialnum','neq',' ')->where('fk_bdl_bd_texturenum','neq',' ')->where('fk_bdl_bd_surfacenum','neq',' ')->whereNull('fk_bdl_bd_surface')->select();}
        if(empty($data)){$data = \app\admin\model\Inventory::where('fk_bdl_bd_materialnum','neq',' ')->where('fk_bdl_bd_texturenum','neq',' ')->where('fk_bdl_bd_surfacenum','neq',' ')->whereNull('fk_bdl_bd_texture')->select();}

        foreach ($data as $k=>$v)
        {
            $update   = [];


            if(!empty($v['fk_bdl_bd_materialnum']))
            {
                $material = Material::where('fnumber',$v['fk_bdl_bd_materialnum'])
                    ->where('fk_bdl_bd_datacenter',$v['fk_bdl_bd_datacenter'])
                    ->find();
                if(!empty($material))
                {
                    $update['fk_bdl_bd_material'] = $material['id'];
                }
            }

            if(!empty($v['fk_bdl_bd_texturenum']))
            {
                $texture = Texture::where('fnumber',$v['fk_bdl_bd_texturenum'])
                    ->where('fk_bdl_bd_datacenter',$v['fk_bdl_bd_datacenter'])
                    ->find();

                if(!empty($texture))
                {
                    $update['fk_bdl_bd_texture'] = $texture['id'];
                }

            }


            if(!empty($v['fk_bdl_bd_surfacenum']))
            {
                $surface = Surface::where('fnumber',$v['fk_bdl_bd_surfacenum'])
                    ->where('fk_bdl_bd_datacenter',$v['fk_bdl_bd_datacenter'])
                    ->find();

                if(!empty($surface))
                {
                    $update['fk_bdl_bd_surface'] = $surface['id'];
                }
            }





            if(!empty($update))
            {
                \app\admin\model\Inventory::where('id',$v['id'])->update($update);
            }
        }

    }

    public function endsWithZero($str) {
        return strlen($str) > 0 && substr($str, -1) === '0';
    }
}



